package com.ls.atf.core.web.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.ls.atf.core.web.WebPage;
import com.ls.atf.core.web.elements.Button;
import com.ls.atf.core.web.elements.TextInput;
import com.ls.atf.data.UserData;

public class LoginPage extends WebPage<LoginPage> {

	private static final String PAGE_URL = "http://dev.mytaglists.com/login.jsp";

	public LoginPage(WebDriver driver) {
		super(driver);
	}

	@Override
	public LoginPage load() {
		driver.get(PAGE_URL);
		return this;
	}

	@Override
	public boolean isAvailable() {
		return getEmailInput().isAvailable() && 
				getPasswordInput().isAvailable() &&
				getLoginButton().isAvailable();
	}
	
	public HomePage loginAs(UserData user) {
		getEmailInput().inputText(user.getEmail());
		getPasswordInput().inputText(user.getPassword());
		getLoginButton().click();
		return new HomePage(driver).waitUntilAvailable();
	}

	private TextInput getEmailInput() {
		return new TextInput(driver, By.name("j_username"));
	}

	private TextInput getPasswordInput() {
		return new TextInput(driver, By.name("j_password"));
	}
	
	private Button getLoginButton() {
		return new Button(driver, By.name("submit"));
	}

}
